package summary;

import java.util.Iterator;
import java.util.Vector;

public class ReferenceStatistics {
	long[] maxSkewToReference = null;
		
	public ReferenceStatistics(Vector<InstantData> instants,int numNodes, long minSecond,long maxSecond){
		maxSkewToReference = new long[numNodes];
		
		for (int i = 0; i < maxSkewToReference.length; i++) {
			maxSkewToReference[i] = 0;
		}
		
		for (Iterator<InstantData> iterator = instants.iterator(); iterator.hasNext();) {
			InstantData instantData = (InstantData) iterator.next();
			if(instantData.second > minSecond && instantData.second < maxSecond)
			for (Iterator<NodeData> iterator2 = instantData.getData().iterator(); iterator2.hasNext();) {
				NodeData node = (NodeData) iterator2.next();
								
				calculateSkewToRoot(node, instantData.getData().firstElement());
			}
		}
	}
	
	private void calculateSkewToRoot(NodeData node, NodeData reference){
		
		long skew = Math.abs(node.clock - reference.clock);
		
		if(maxSkewToReference[(int) (node.id-1)] < skew)
			maxSkewToReference[(int) (node.id-1)] = skew;
	}
	
	public long[] getReferenceSkews(){
		return maxSkewToReference;
	}
}
